<?php

namespace Apps\Manage\Controllers;

use Phalcon\Http\Response;
use PhalconPlus\Common\ResponseResult;
use PhalconPlus\Common\ResponseResultStatus;
use PhalconPlus\Operator\GroupRight as OperatorGroupRight;
use Apps\Common\Models\BackendOperatorLog;
use Phalcon\Mvc\Controller;
use PhalconPlus\Operator\Identity as OperatorIdentity;
use PhalconPlus\Common\Util;
use Apps\Common\Libs\UploadPic;
use PhalconPlus\Operator\Identity;

class BaseController extends Controller
{
    /**
     * 每页现实数量
     * @var int
     */
    public $pageNavLimit = 20;
    /**
     * @var \PhalconPlus\Operator\Identity
     */
    public $operator;

    public $isVerifyToken = true;

    public $_response = null;

    public function initialize()
    {

        $this->operator = new Identity();
        if($this->request->isPost() && $this->isVerifyToken && !$this->security->checkToken()) {
            $this->response->redirect("index/show401");
        }


        $moduleConfig = $this->di->get('moduleConfig');

        $this->tag->setTitle($moduleConfig->application->id);

        $this->pageNavLimit = $moduleConfig->application->offsetExists('pageLimit') ? $moduleConfig->application->pageLimit : $this->pageNavLimit;


        $this->view->setVar('main_nav_type', '');
        $this->view->setVar('left_nav_type', '');
    }

    /**
     * 数据库操作异常
     * @param \Phalcon\Mvc\Model $model
     * @return bool
     */
    public function databaseErrorLog($model)
    {
        $error = '';
        foreach($model->getMessages() as $msg)
        {
            $error .= $msg . ',';
        }
        $this->di->get('logger')->error('数据库异常, GET:' . json_encode($_GET) . ', POST:' . json_encode($_POST) . ', error: ' . $error);
        return false;
    }

    /**
     * 设置主导航类型
     * @param $type
     */
    public function setMainNav($type) {
        $this->view->setVar('main_nav_type', $type);
    }

    /**
     * 设置左侧导航类型
     * @param $type
     */
    public function setLeftNav($type) {
        $this->view->setVar('left_nav_type', $type);
    }

    /**
     * 设置相应内容
     * @param $content
     * @return Response
     */
    public function setResponseContent($content)
    {
        if($this->_response == null) {
            $this->_response = new Response();
        }
        $this->_response->setContent($content);
        return $this->_response;
    }

    /**
     * @return Response
     */
    public function getResponse()
    {
        return $this->_response;
    }
}